package mysgoog.util;

import java.io.*;
import java.util.*;

import mysgoog.gtp.Vertex;
/**
* @author Fu, Zhongyang
*/
public class InfluenceDBReader
{
	String name;
    List<String> pattern = new ArrayList<String>();
    String s = "";
    String ans = "";
    String ansud = "";
    String anslr = "";
    String ansudlr = "";
    int l = 0;
    int w = 0;
    int x = 0;
    int y = 0;
    int defencex = 0;
    int defencey = 0;
    boolean hasDefence = false;
    int count = 0;
    Map<String, Vertex> characterMap = new TreeMap<String, Vertex>();

    public static void main(String arg[])
    {
        new InfluenceDBReader("pattern\\makeeye.db");
    }

    public InfluenceDBReader(String filename)
    {
        read(filename);
        FileManager.write("pattern\\makeeye.dbpattern", s);
    }

    public void read(String filename)
    {
        BufferedReader reader = null;
        try
        {
            File file = new File(filename);
            reader = new BufferedReader(new FileReader(file));
            String text = null;
            while ((text = reader.readLine()) != null)
            {
                //System.err.println(text);
            	//System.out.println(count++);
                parse(text);
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (reader != null)
                {
                    reader.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    public void parse(String content){
    	if (content.length() == 0) return;
    	String[] token = content.split(" ");
    	switch (token[0].charAt(0)){
    		case '#':
    			return;
    		case ':':
    			s += patternSize(0) + " " + parseValue(content) + " " + ans + "\n";
    			s += patternSize(1) + " " + parseValue(content) + " " + ansud + "\n";
    			s += patternSize(2) + " " + parseValue(content) + " " + anslr + "\n";
    			s += patternSize(3) + " " + parseValue(content) + " " + ansudlr + "\n";
    			if (hasDefence) {    				
    				parseDefence();
    				//s += "defence \n";
        			s += patternDefenceSize(0) + " " + parseValue(content) + " " + ans + "\n";
        			s += patternDefenceSize(1) + " " + parseValue(content) + " " + ansud + "\n";
        			s += patternDefenceSize(2) + " " + parseValue(content) + " " + anslr + "\n";
        			s += patternDefenceSize(3) + " " + parseValue(content) + " " + ansudlr + "\n";
    			}
    			return;
    		case ';':
    			//
    			return;
    		case 'P':
    			ans = "";
    			ansud = "";
    			anslr = "";
    			ansudlr = "";
    			l = 0;
    			w = 0;
    			hasDefence = false;
    			name = token[1];
    		    characterMap = new TreeMap<String, Vertex>();
    			return;
    		case '>':
    			return;
    		default : 
    			parsePattern(content);
    			return;
    	}
    	
    }
    
    public String parseValue(String content){    	
    	//System.out.println(content);
    	String[] token = content.split(",");
    	String ans = token[2].substring(6, token[2].length() - 1);    	
    	return ans;
    	//return "100";
    }
    
    public void parsePattern(String content){    	
    	content = content.replace('-', '|');
    	content = content.replace('+', '|');    	
    	if (content.contains("*")) {
    		x = l;
    		for (int i = 0; i < content.length(); i++){
    			if (content.charAt(i) == '*') {
    				y = i;    				
    				break;
    			}
    		}
    		content = content.replace('*', '.');
    	}
    	if (content.contains("!")) {
    		defencex = l;
    		for (int i = 0; i < content.length(); i++){
    			if (content.charAt(i) == '!') {
    				defencey = i;    				
    				break;
    			}
    		}
    		content = content.replace('!', '.');
    		hasDefence = true;
    	}
    	w = content.length();
    	l++;
    	ans += content;
    	ansud = content + ansud;
    	StringBuilder builder = new StringBuilder(content);
    	builder.reverse();
    	anslr += builder;
    	ansudlr = builder + ansudlr;
    }
    
    public String patternSize(int i){
    	switch (i){
    		case 0:
    			return l + " " + w + " " + x + " " + y;
    		case 1:
    			return l + " " + w + " " + (l - 1 - x) + " " + y; 
    		case 2:
    			return l + " " + w + " " + x + " " + (w - 1 - y);
    		case 3:
    			return l + " " + w + " " + (l - 1 - x) + " " + (w - 1 - y);
    	}
    	return null;
    }
    
    public String patternDefenceSize(int i){
    	switch (i){
    		case 0:
    			return l + " " + w + " " + defencex + " " + defencey;
    		case 1:
    			return l + " " + w + " " + (l - 1 - defencex) + " " + defencey; 
    		case 2:
    			return l + " " + w + " " + defencex + " " + (w - 1 - defencey);
    		case 3:
    			return l + " " + w + " " + (l - 1 - defencex) + " " + (w - 1 - defencey);
    	}
    	return null;
    }
    
    private void parseDefence(){
    	ans = ans.replace('O', 'P');
    	ans = ans.replace('o', 'p');
    	ans = ans.replace('X', 'O');
    	ans = ans.replace('x', 'o');
    	ans = ans.replace('P', 'X');
    	ans = ans.replace('p', 'x');
    	

    	ansud = ansud.replace('O', 'P');
    	ansud = ansud.replace('o', 'p');
    	ansud = ansud.replace('X', 'O');
    	ansud = ansud.replace('x', 'o');
    	ansud = ansud.replace('P', 'X');
    	ansud = ansud.replace('p', 'x');
    	

    	anslr = anslr.replace('O', 'P');
    	anslr = anslr.replace('o', 'p');
    	anslr = anslr.replace('X', 'O');
    	anslr = anslr.replace('x', 'o');
    	anslr = anslr.replace('P', 'X');
    	anslr = anslr.replace('p', 'x');
    	

    	ansudlr = ansudlr.replace('O', 'P');
    	ansudlr = ansudlr.replace('o', 'p');
    	ansudlr = ansudlr.replace('X', 'O');
    	ansudlr = ansudlr.replace('x', 'o');
    	ansudlr = ansudlr.replace('P', 'X');
    	ansudlr = ansudlr.replace('p', 'x');
    }
    
    public void parseLib(String content){
    	content = content.substring(1);
    	String[] token = content.split(" ");
    	for (int i = 0; i < token.length; i++){
    		if (token[i].contains("lib")) parseLibString(token[i]);
    	}
    }
    
    public void parseLibString(String lib){
    	int pos = lib.indexOf("lib");
    	
    }
    
}
